package com.cdgeekcamp.redas.wechat.service;

import com.cdgeekcamp.redas.wechat.component.CommonClassGetBean;
import com.cdgeekcamp.redas.wechat.entity.*;

import java.util.Date;

/**
 * 消息处理类
 */
public class MsgHandlerService {
    /**
     * 事件消息
     * @param receiveMsg
     * @return
     */
    public String eventHandler(EventMessageEntity receiveMsg) {
        TextMessageEntity outMsg = new TextMessageEntity();

        outMsg.setFromUserName(receiveMsg.getToUserName());
        outMsg.setToUserName(receiveMsg.getFromUserName());
        outMsg.setMsgType("text");
        outMsg.setCreateTime(new Date().getTime());

        String url, reply = "敬请期待";

        switch (receiveMsg.getEventKey()) {
            case "sub":
                url = CommonClassGetBean.getBean("getDoMain") + "/getSub?openid=" + receiveMsg.getFromUserName() + "&type=wechat";
                reply = String.format("<a href=\"%s\">想要查看订阅列表，点我</a>", url);
                break;
            case "month_chart":
            case "week_chart":
                outMsg.setContent("https://www.baidu.com");
                break;
            case "position_list":
                url = CommonClassGetBean.getBean("getDoMain") + "/?openid=" + receiveMsg.getFromUserName() + "&type=wechat";
                reply = String.format("<a href=\"%s\">想要查看职位列表，点我</a>", url);
                break;
            case "position_analyze": {
                url = CommonClassGetBean.getBean("getDoMain") + "/charts?openid=" + receiveMsg.getFromUserName() + "&type=wechat";
                reply = String.format("<a href=\"%s\">想要查看职位分析，点我</a>", url);
                break;
            }
        }

        outMsg.setContent(reply);

        return new XmlProcess().ToXml(outMsg);
    }

    /**
     * 文本消息
     * @param receiveMsg
     * @return
     */
    public String textHandler(TextMessageEntity receiveMsg){
        TextMessageEntity outMsg = new TextMessageEntity();

        outMsg.setFromUserName(receiveMsg.getToUserName());
        outMsg.setToUserName(receiveMsg.getFromUserName());
        outMsg.setMsgType("text");
        outMsg.setCreateTime(new Date().getTime());
        outMsg.setContent(receiveMsg.getContent());

        return new XmlProcess().ToXml(outMsg);
    }

    /**
     * 图片消息
     * @param receiveMsg
     * @return
     */
    public String imageHandler(ImageMessageEntity receiveMsg){
        return notSupportHandler(receiveMsg);
    }

    /**
     * 链接消息
     * @param receiveMsg
     * @return
     */
    public String linkHandler(LinkMessageEntity receiveMsg){
        return notSupportHandler(receiveMsg);
    }

    /**
     * 地理位置消息
     * @param receiveMsg
     * @return
     */
    public String locationHandler(LocationMessageEntity receiveMsg){
        return notSupportHandler(receiveMsg);
    }

    /**
     * 短视频消息
     * @param receiveMsg
     * @return
     */
    public String shortVideoHandler(ShortVideoMessageEntity receiveMsg){
        return notSupportHandler(receiveMsg);
    }

    /**
     * 视频消息
     * @param receiveMsg
     * @return
     */
    public String videoHandler(VideoMessageEntity receiveMsg){
        return notSupportHandler(receiveMsg);
    }

    /**
     * 语音消息
     * @param receiveMsg
     * @return
     */
    public String voiceHandler(VoiceMessageEntity receiveMsg){
        return notSupportHandler(receiveMsg);
    }

    /**
     * 不明消息
     * @param receiveMsg
     * @return
     */
    public String unknownHandler(UnknownMessageEntity receiveMsg){
        TextMessageEntity outMsg = new TextMessageEntity();

        outMsg.setFromUserName(receiveMsg.getToUserName());
        outMsg.setToUserName(receiveMsg.getFromUserName());
        outMsg.setMsgType("text");
        outMsg.setCreateTime(new Date().getTime());
        outMsg.setContent("无效消息或动作");

        return new XmlProcess().ToXml(outMsg);
    }

    /**
     * 暂不支持消息处理
     * @param receiveMsg
     * @return
     */
    public String notSupportHandler(MessageBaseEntity receiveMsg){
        TextMessageEntity outMsg = new TextMessageEntity();

        outMsg.setFromUserName(receiveMsg.getToUserName());
        outMsg.setToUserName(receiveMsg.getFromUserName());
        outMsg.setMsgType("text");
        outMsg.setCreateTime(new Date().getTime());
        outMsg.setContent("系统暂不支持此类消息");

        return new XmlProcess().ToXml(outMsg);
    }
}
